#include "ipc_timestamp.h"

#include <time.h>

void ipcGetTime(ipcTimestamp_s *pTimestamp)
{
	struct timespec tp= {0,0};
	int clocktime = 0;
	
	if(NULL == pTimestamp)
	{
		return;
	}

	clocktime = clock_gettime(CLOCK_MONOTONIC, &tp);

	if(0 == clocktime)
	{
		pTimestamp->second = tp.tv_sec;
		pTimestamp->nanosecond = tp.tv_nsec;
		
		//printf("second %d nanosecond %d \n",pIpc_time->second,pIpc_time->nanosecond);
	}
	else
	{
		pTimestamp->second = pTimestamp->nanosecond = 0;
	}
	
	return;
}

void ipcTimestampAddMilisecond(ipcTimestamp_s *pTimestamp,int millisecond)
{
	int secondAdd = 0;
	int nanoSecondAdd = 0;
	
	if(NULL == pTimestamp)
	{
		return;
	}

	secondAdd = millisecond / MILLISECOND_IN_SECOND;
	nanoSecondAdd = (millisecond % MILLISECOND_IN_SECOND) * NANOSECOND_IN_MILLISECOND;

	pTimestamp->nanosecond += nanoSecondAdd;
	pTimestamp->second += secondAdd;
	
	if(pTimestamp->nanosecond > NANOSECOND_IN_SECOND)
	{
		pTimestamp->nanosecond -= NANOSECOND_IN_SECOND;
		pTimestamp->second++;
	}	
	
	return;
}

